﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Globalization;
using System.Runtime.InteropServices;
using GaSaiGon.Report;

namespace GaSaiGon.GUI
{
    public partial class frmThemVe : Form
    {
        public frmThemVe()
        {
            InitializeComponent();
            this.btnThoat.Click += new EventHandler(btnThoat_Click);
        }

        public void LoadCombobox()
        {
            ThongTinBUS TTBus = new ThongTinBUS();
            cbxLoaiChuyen.DataSource = TTBus.LoaiChuyen();
            cbxLoaiChuyen.ValueMember = "MaLoaiChuyen";
            cbxLoaiChuyen.DisplayMember = "TenLoaiChuyen";

            cbxDiaDiemDi.DataSource = TTBus.DiaDiem();
            cbxDiaDiemDi.ValueMember = "MaDiaDiem";
            cbxDiaDiemDi.DisplayMember = "TenDiaDiem";

            cbxDiaDiemDen.DataSource = TTBus.DiaDiem();
            cbxDiaDiemDen.ValueMember = "MaDiaDiem";
            cbxDiaDiemDen.DisplayMember = "TenDiaDiem";

            cbxLoaiGhe.DataSource = TTBus.LoaiGhe();
            cbxLoaiGhe.ValueMember = "IDLoaiGhe";
            cbxLoaiGhe.DisplayMember = "TenLoaiGhe";

            cbxDoiTuong.DataSource = TTBus.DoiTuong();
            cbxDoiTuong.ValueMember = "MaDoiTuong";
            cbxDoiTuong.DisplayMember = "LoaiDoiTuong";

            cbxGioiTinh.DataSource = TTBus.GioiTinh();
            cbxGioiTinh.ValueMember = "MaGioiTinh";
            cbxGioiTinh.DisplayMember = "LoaiGioiTinh";
        }
        private void frmThemVe_Load(object sender, EventArgs e)
        {
            LoadCombobox();
            tbMuaVe.Controls.Remove(tpKhachHang);
            tbMuaVe.Controls.Remove(tpThanhToan);
        }
        public void DanhSachCacNoiDiQua()
        {
            ThongTinBUS TTBus = new ThongTinBUS();
            ThongTinDTO TTDto = new ThongTinDTO();
            TTDto.DanhSachCacNoiDiQua(int.Parse(cbxDiaDiemDi.SelectedValue.ToString()), int.Parse(cbxDiaDiemDen.SelectedValue.ToString()));
            DataTable dt = new DataTable();
            dt = TTBus.DanhSachCacNoiDiQua(TTDto);
            lbDanhSachDiQua.Text = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lbDanhSachDiQua.Text = lbDanhSachDiQua.Text + " -> " + dt.Rows[i]["TenDiaDiem"].ToString();
            }
        }
        private void btnChon_Click_1(object sender, EventArgs e)
        {
            if (cbxDiaDiemDen.SelectedIndex == cbxDiaDiemDi.SelectedIndex)
            {
                MessageBox.Show("Địa điểm đi và địa điểm đến phải khác nhau !");
                return;
            }
            ThongTinBUS TTBus = new ThongTinBUS();
            ThongTinDTO TTDto = new ThongTinDTO();
            TTDto.LoaiChuyen = int.Parse(cbxLoaiChuyen.SelectedValue.ToString());
            TTDto.DiaDiemDi = int.Parse(cbxDiaDiemDi.SelectedValue.ToString());
            TTDto.DiaDiemDen = int.Parse(cbxDiaDiemDen.SelectedValue.ToString());
            TTDto.LoaiGhe = int.Parse(cbxLoaiGhe.SelectedValue.ToString());
            dgvDanhSach.DataSource = TTBus.ThongTinChuyenTau(TTDto);
            if (dgvDanhSach.Rows.Count == 1)
            {
                MessageBox.Show("Không có chuyến tàu nào ! ");
            }
        }
        int GiaVe;
        private void dgvDanhSach_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            lbMaTau.Text = dgvDanhSach.CurrentRow.Cells["MaTau"].Value.ToString();
            lbSoToa.Text = dgvDanhSach.CurrentRow.Cells["SoToa"].Value.ToString();
            lbSoCho.Text = dgvDanhSach.CurrentRow.Cells["SoLuong"].Value.ToString();
            lbLoaiGhe.Text = dgvDanhSach.CurrentRow.Cells["TenLoaiGhe"].Value.ToString();
            string NgayKhoiHanh = String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Parse(dgvDanhSach.CurrentRow.Cells["NgayKhoiHanh"].Value.ToString()));
            lbNgayKhoiHanh.Text = NgayKhoiHanh.ToString();
            DateTime dtNgayDen = DateTime.Parse(dgvDanhSach.CurrentRow.Cells["NgayKhoiHanh"].Value.ToString());
            DateTime test = dtNgayDen.AddMinutes(int.Parse(dgvDanhSach.CurrentRow.Cells["ThoiGian"].Value.ToString()));
            string NgayDen = String.Format("{0:d/M/yyyy HH:mm:ss}", test);
            lbNgayDen.Text = NgayDen.ToString();                      
            GiaVe = int.Parse(dgvDanhSach.CurrentRow.Cells["GiaGhe"].Value.ToString()) + int.Parse(dgvDanhSach.CurrentRow.Cells["GiaChuyen"].Value.ToString());
            lbGiaVe.Text = GiaVe.ToString() + " VND";
            lbThoiGian.Text = dgvDanhSach.CurrentRow.Cells["ThoiGian"].Value.ToString() + "phút";
            lbMaChuyenTau.Text = dgvDanhSach.CurrentRow.Cells["IDChuyenTau"].Value.ToString();
            DanhSachCacNoiDiQua();
        }
        private void btnDatMua_Click(object sender, EventArgs e)
        {
            if (lbMaTau.Text == "Chưa có")
            {
                MessageBox.Show("Chưa chọn chuyến tàu !");
                cbxDiaDiemDen.Focus();
                return;
            }
            txtHoTen.BackColor = Color.White;
            txtCMND.BackColor = Color.White;
            txtDiaChi.BackColor = Color.White;
            txtDienThoai.BackColor = Color.White;
            txtSoLuongNguoi.BackColor = Color.White;
            if (txtHoTen.Text == "")
            {
                MessageBox.Show("Nhập họ tên !");
                txtHoTen.Focus();
                txtHoTen.BackColor = Color.Orange;
                return;
            }
            if (txtCMND.Text == "")
            {
                MessageBox.Show("Nhập số chứng min nhân dân !");
                txtCMND.Focus();
                txtCMND.BackColor = Color.Orange;
                return;
            }
            if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Nhập địa chỉ !");
                txtDiaChi.Focus();
                txtDiaChi.BackColor = Color.Orange;
                return;
            }
            if (txtDienThoai.Text == "")
            {
                MessageBox.Show("Nhập số điện thoại !");
                txtDienThoai.Focus();
                txtDienThoai.BackColor = Color.Orange;
                return;
            }
            if (cbVeTapThe.Checked == true)
            {
                if (txtSoLuongNguoi.Text == "")
                {
                    MessageBox.Show("Nhập vào số lượng người !");
                    txtSoLuongNguoi.Focus();
                    txtSoLuongNguoi.BackColor = Color.Orange;
                    return;
                }
            }
            int SoLuongNguoi = 1;
            lbSoLuongNguoi.Text = "1";
            tbMuaVe.Controls.Add(tpKhachHang);
            lbHoTen.Text = txtHoTen.Text;
            lbDiaChi.Text = txtDiaChi.Text;
            lbDienThoai.Text = txtDienThoai.Text;
            lbCMND.Text = txtCMND.Text;
            tbMuaVe.SelectedTab = tpKhachHang;
            lbThongTinGiamGia.Text = "Không giảm giá";
            lbSoTienGiam.Text = "0";
            lbGiamVeTapThe.Text = "Không giảm giá";
            lbSoTienGiamVeTapThe.Text = "0";
            lblVeTapThe.Text = "Không";
            int TongGia = GiaVe;
            lbTongGiaVe.Text = TongGia.ToString();

            DateTime dtThoiGianHienTai = System.DateTime.Now;
            DateTime dtThoiGianKhoiHanh = DateTime.Parse(dgvDanhSach.CurrentRow.Cells["NgayKhoiHanh"].Value.ToString());
            TimeSpan ThoiGianMuaVe = dtThoiGianKhoiHanh - dtThoiGianHienTai;
            lbThoiGianMuaVe.Text = ThoiGianMuaVe.Days.ToString() + " ngày";


            int GiamGia;
            int GiaChinh = GiaVe;
            int TongThoiGianMuaVe = int.Parse(ThoiGianMuaVe.Days.ToString());
            lbSoTienGiamVe.Text = "0";
            if (TongThoiGianMuaVe >= 60)
            {
                lbGiamMuaVe.Text = "Giảm 20%";
                GiamGia = (GiaVe * 20) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbSoTienGiamVe.Text = GiamGia.ToString();
                lbTongGiaVe.Text = GiaChinh.ToString();
                GiaVe = GiaChinh;
            }
            if (TongThoiGianMuaVe >= 15 && TongThoiGianMuaVe <= 59)
            {
                lbGiamMuaVe.Text = "Giảm 10%";
                GiamGia = (GiaVe * 10) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbSoTienGiamVe.Text = GiamGia.ToString();
                lbTongGiaVe.Text = GiaChinh.ToString();
                GiaVe = GiaChinh;
            }

            
            if (cbVeTapThe.Checked == true)
            {
                SoLuongNguoi = int.Parse(txtSoLuongNguoi.Text);
                GiaVe = GiaVe * SoLuongNguoi;
                int GiamGiaTapThe;
                int GiaChinhTapThe = GiaVe;
                lblVeTapThe.Text = "Có";
                cbxDoiTuong.Enabled = false;
                if (SoLuongNguoi >= 20 && SoLuongNguoi <= 29)
                {
                    lbGiamVeTapThe.Text = "Giảm 2%";
                    GiamGiaTapThe = (GiaVe * 2) / 100;
                    GiaChinhTapThe = GiaChinhTapThe - GiamGiaTapThe;
                    lbSoTienGiamVeTapThe.Text = GiamGiaTapThe.ToString();
                    lbTongGiaVe.Text = GiaChinhTapThe.ToString();
                    lbSoLuongNguoi.Text = SoLuongNguoi.ToString();
                }
                if (SoLuongNguoi >= 30)
                {
                    lbGiamVeTapThe.Text = "Giảm 5%";
                    GiamGiaTapThe = (GiaVe * 5) / 100;
                    GiaChinhTapThe = GiaChinhTapThe - GiamGiaTapThe;
                    lbSoTienGiamVeTapThe.Text = GiamGiaTapThe.ToString();
                    lbTongGiaVe.Text = GiaChinhTapThe.ToString();
                    lbSoLuongNguoi.Text = SoLuongNguoi.ToString();
                }
            }

            
        }

        private void cbxDoiTuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            int loaichuyen = int.Parse(cbxLoaiChuyen.SelectedValue.ToString());
            int GiamGia;
            int GiaChinh = GiaVe;
            int chon = int.Parse(cbxDoiTuong.SelectedValue.ToString());
            if (chon == 1)
            {
                lbThongTinGiamGia.Text = "Không giảm giá";
                lbSoTienGiam.Text = "0";
                lbTongGiaVe.Text = GiaVe.ToString();
            }
            if (chon == 2)
            {
                lbThongTinGiamGia.Text = "Giảm 10%";
                GiamGia = (GiaChinh * 10) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
            if (chon == 3)
            {
                lbThongTinGiamGia.Text = "Giảm 5%";
                GiamGia = (GiaChinh * 5) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
            if (chon == 4)
            {
                lbThongTinGiamGia.Text = "Giảm 50%";
                GiamGia = (GiaChinh * 50) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
            if (chon == 5 && loaichuyen == 2)
            {
                lbThongTinGiamGia.Text = "Giảm 10%";
                GiamGia = (GiaChinh * 10) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
            if (chon == 5 && loaichuyen == 3)
            {
                lbThongTinGiamGia.Text = "Giảm 25%";
                GiamGia = (GiaChinh * 25) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
            if (chon == 5)
            {
                lbThongTinGiamGia.Text = "Không giảm giá";
                lbSoTienGiam.Text = "0";
            }
            if (chon == 6)
            {
                lbThongTinGiamGia.Text = "Giảm 90%";
                GiamGia = (GiaChinh * 90) / 100;
                GiaChinh = GiaChinh - GiamGia;
                lbTongGiaVe.Text = GiaChinh.ToString();
                lbSoTienGiam.Text = GiamGia.ToString();
            }
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            tbMuaVe.Controls.Add(tpThanhToan);
            tbMuaVe.SelectedTab = tpThanhToan;
            ThongTinBUS TTBus = new ThongTinBUS();
            ThongTinDTO TTDto = new ThongTinDTO();
            string HoTen = txtHoTen.Text.ToString();
            int GioiTinh = int.Parse(cbxGioiTinh.SelectedValue.ToString());
            string CMND = txtCMND.Text.ToString();
            string DiaChi = txtDiaChi.Text.ToString();
            string DienThoai = txtDienThoai.Text.ToString();
            int DoiTuong = int.Parse(cbxDoiTuong.SelectedValue.ToString());
            int GiaVe = int.Parse(lbTongGiaVe.Text.ToString());
            int MaChuyenTau = int.Parse(lbMaChuyenTau.Text.ToString());

            DateTime dtNgayDen = DateTime.Parse(dgvDanhSach.CurrentRow.Cells["NgayKhoiHanh"].Value.ToString());
            DateTime test = dtNgayDen.AddMinutes(int.Parse(dgvDanhSach.CurrentRow.Cells["ThoiGian"].Value.ToString()));
            DateTime NgayDen = test;

            int VeTapThe = 0;
            if (cbVeTapThe.Checked == true)
            {
                VeTapThe = 1;
            }
            int SoLuongNguoiTT = int.Parse(lbSoLuongNguoi.Text);
            int LoaiGhe = int.Parse(cbxLoaiGhe.SelectedValue.ToString());
            TTDto.ThongTinThanhToan(HoTen, GioiTinh, CMND, DiaChi, DienThoai, DoiTuong, GiaVe, MaChuyenTau, NgayDen,
                VeTapThe, SoLuongNguoiTT,LoaiGhe,1);
            DataTable dt = new DataTable();
            dt = TTBus.ThongTinThanhToan(TTDto);
            lbMaVe.Text = dt.Rows[0]["MaVe"].ToString();
        }

        private void btnInHoaDon_Click(object sender, EventArgs e)
        {
            frmHoaDon frm = new frmHoaDon();
            crHoaDon cr = new crHoaDon();
            cr.SetParameterValue("MaVe", lbMaVe.Text);
            cr.SetParameterValue("DiaDiemDi", cbxDiaDiemDi.SelectedValue);
            cr.SetParameterValue("DiaDiemDen", cbxDiaDiemDen.SelectedValue);
            frm.KhoiTaoReport(cr);
            frm.Show();
        }

        private void btnInVe_Click(object sender, EventArgs e)
        {
            frmHoaDon frm = new frmHoaDon();
            crVe cr = new crVe();
            cr.SetParameterValue("MaVe", lbMaVe.Text);
            cr.SetParameterValue("DiaDiemDi", cbxDiaDiemDi.SelectedValue);
            cr.SetParameterValue("DiaDiemDen", cbxDiaDiemDen.SelectedValue);
            frm.KhoiTaoReport(cr);
            frm.Show();
        }
        public delegate void SEND(string s);
        public SEND sender;
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.sender("Close");
            Close();
        }

        private void btnTiepTucMuaVe_Click(object sender, EventArgs e)
        {
            tbMuaVe.Controls.Remove(tpKhachHang);
            tbMuaVe.Controls.Remove(tpThanhToan);
        }

        private void frmThemVe_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.sender("Close");
        }
    }
}
